package com.zscat.mallplus.center.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zscat.mallplus.center.entity.CenterOrderRecharge;
import com.zscat.mallplus.center.service.ICenterOrderRechargeService;
import com.zscat.mallplus.controller.ApiBaseAction;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/center/order/recharge")
public class CenterOrderRechargeController extends ApiBaseAction {
	@Autowired
	private ICenterOrderRechargeService centerOrderRechargeService;
	@Autowired
	private IUmsMemberService memberService;

	/**
	 * 新增订单-会员充值
	 */
	@PostMapping("add")
	public Object add(@RequestBody CenterOrderRecharge centerOrderRecharge) {
		try {
			UmsMember user = memberService.getNewCurrentMember();
			centerOrderRecharge.setMemberId(user.getId());
			centerOrderRecharge.setIcon(user.getIcon());
			centerOrderRecharge.setNickname(user.getNickname());
			return centerOrderRechargeService.add(centerOrderRecharge);
		} catch (Exception e) {
			log.error("新增订单-会员充值：%s", e.getMessage(), e);
		}
		return new CommonResult().failed();
	}

	/**
	 * 微信统一支付下单接口
	 * 
	 * @param orderId
	 * @param appIdsource
	 * @return
	 */
	@PostMapping("createOrder")
	public Object createOrder(@RequestParam(value = "orderId", required = false, defaultValue = "0") Long orderId,
			@RequestParam(value = "appIdsource", required = false) Integer appIdsource) {
		return centerOrderRechargeService.createOrder(orderId, appIdsource);
	}

	/**
	 * 微信订单回调接口
	 *
	 * @return
	 */
	@PostMapping("notify")
	public void notify(HttpServletRequest request, HttpServletResponse response) {
		centerOrderRechargeService.notify(request, response);
	}
}
